package com.xysq.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rockcent.model.CouponBO;
import com.xysq.XYApplication;
import com.xysq.activity.CommodityDetailsActivity;
import com.xysq.activity.CustomerCouponDetailActivity;
import com.xysq.activity.TipsActivity;
import com.xysq.lemon.R;
import com.xysq.util.PropertiesUtil;
import com.xysq.util.UseUtil;

import butterknife.InjectView;

/**
 * Created by huangdongkai on 2015/7/22.
 */
@SuppressLint("ValidFragment")
public class TicketFragment extends BaseFragment implements View.OnClickListener {
    private CouponBO couponBO;
    @InjectView(R.id.iv_first_pic)
    ImageView firstPicIv;
    @InjectView(R.id.iv_second_pic)
    ImageView secondPicIv;
    @InjectView(R.id.txt_customer_name)
    TextView nameTxt;
    @InjectView(R.id.txt_price)
    TextView priceTxt;
    @InjectView(R.id.txt_introduce)
    TextView introduceTxt;
    @InjectView(R.id.txt_to_detail)
    TextView toDetailTxt;
    /*    @InjectView(R.id.iv_pic)
        ImageView picIv;*/
    @InjectView(R.id.layout_pic)
    LinearLayout picLayout;
    @InjectView(R.id.layout_data)
    LinearLayout dataLayout;

    public TicketFragment(CouponBO couponBO) {
        this.couponBO = couponBO;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflaterView(inflater, R.layout.fragment_tickets_list, null);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        WindowManager wm = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        Log.d("test", width + "width");
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width - 150, (width - 50) / 2);
        layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
//        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams((width - 100), (width - 50) / 2);
        layoutParams.setMargins(10, 5, 10, 5);
        firstPicIv.setLayoutParams(layoutParams);
        secondPicIv.setLayoutParams(layoutParams);
//        layoutParams = new LinearLayout.LayoutParams(width - 150, width - 28);
//        layoutParams.setMargins(10, 10, 10, 10);
//        picIv.setLayoutParams(layoutParams);
        if (couponBO != null) {
            String picUrl = couponBO.getPicUrl();
            if (null != picUrl) {
                if (picUrl.indexOf(";") != -1) {
                    XYApplication.getImageLoader().displayImage(PropertiesUtil.getImageServer() + picUrl.substring(0, picUrl.indexOf(";")), firstPicIv);
//                    XYApplication.getImageLoader().displayImage(PropertiesUtil.getImageServer() + picUrl.substring(0, picUrl.indexOf(";")), picIv);
                    picUrl = picUrl.substring(picUrl.indexOf(";") + 1);
                    if (picUrl.indexOf(";") != -1) {
                        XYApplication.getImageLoader().displayImage(PropertiesUtil.getImageServer() + picUrl.substring(0, picUrl.indexOf(";")), secondPicIv);
                    } else {
                        if (TextUtils.isEmpty(picUrl)) {
                            picUrl = couponBO.getPicUrl();
                            XYApplication.getImageLoader().displayImage(PropertiesUtil.getImageServer() + picUrl.substring(0, picUrl.indexOf(";")), secondPicIv);
                        } else {
                            XYApplication.getImageLoader().displayImage(PropertiesUtil.getImageServer() + picUrl, secondPicIv);
                        }
                    }
                } else {
                    XYApplication.getImageLoader().displayImage(PropertiesUtil.getImageServer() + picUrl, firstPicIv);
                    XYApplication.getImageLoader().displayImage(PropertiesUtil.getImageServer() + picUrl, secondPicIv);
//                    XYApplication.getImageLoader().displayImage(PropertiesUtil.getImageServer() + picUrl, picIv);
                }
            }
            nameTxt.setText(couponBO.getName());
            if (null != couponBO.getIntroduce()) {
                String introduce = couponBO.getIntroduce();
                if (introduce.length() > 30) {
                    introduceTxt.setText(introduce.substring(0, 30) + "...");
                } else {
                    introduceTxt.setText(introduce);
                }
                if (couponBO.getCategory().equals("B")) {
                    priceTxt.setText("￥" + UseUtil.handlePrice(couponBO.getEstimateAmount()));
/*                    picIv.setVisibility(View.GONE);
                    picLayout.setVisibility(View.VISIBLE);*/
                } else if (couponBO.getCategory().equals("C")) {
                    priceTxt.setText("￥" + UseUtil.handlePrice(couponBO.getPrice()));
/*                    picIv.setVisibility(View.VISIBLE);
                    picLayout.setVisibility(View.GONE);*/
                }
           /* toDetailTxt.setOnClickListener(this);
            introduceTxt.setOnClickListener(this);
            nameTxt.setOnClickListener(this);
            firstPicIv.setOnClickListener(this);
            secondPicIv.setOnClickListener(this);
            picIv.setOnClickListener(this);*/
                dataLayout.setOnClickListener(this);
            }
        } else {
            getActivity().finish();
        }
    }

    public void setCouponModel(CouponBO couponBO) {
        this.couponBO = couponBO;
    }

    @Override
    public void onClick(View view) {
        if (couponBO.getCategory().equals("B")) {
            Log.e("++++哪个端券", "B");
            Intent intent = new Intent(getActivity(), CommodityDetailsActivity.class);
            intent.putExtra(CommodityDetailsActivity.COUPONMODEL_ID, couponBO.getId());
            startActivity(intent);

        }
        if (couponBO.getCategory().equals("C")) {
            Log.e("++++哪个端券", "C");
            if(couponBO.getIssueCount().equals("0")) {
                Intent intent = new Intent(getActivity(), TipsActivity.class);
                intent.putExtra(TipsActivity.COUPONMODEL_ID, couponBO.getId());
                intent.putExtra(TipsActivity.CUSTOMER_ID, couponBO.getCustomerId());
                startActivity(intent);
            }else {
                Intent intent = new Intent(getActivity(), TipsActivity.class);
//                Intent intent = new Intent(getActivity(), CustomerCouponDetailActivity.class);
                intent.putExtra(CustomerCouponDetailActivity.COUPONMODEL_ID, couponBO.getId());
                intent.putExtra(CustomerCouponDetailActivity.CUSTOMER_ID, couponBO.getCustomerId());
                startActivity(intent);
            }
        }
    }
}
